package JiHe;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class Test3 {
    //自己定制一个排序要先连接接口然后重写接口的方法
    public static void main(String[] args) {
        Person p1 = new Person("张三",16);
        Person p2 = new Person("李四",10);
        Person p3 = new Person("王五",1);
        Person p4 = new Person("顾新宇",2);
        Set<Person> set =new TreeSet<Person>(new Person());
        set.add(p1);
        set.add(p2);
        set.add(p3);
        set.add(p4);
//        for(Person p:set){
//            System.out.println(p.name+" "+p.age);
//        }
        Iterator<Person> it = set.iterator();
        while (it.hasNext()){
//            Person p = it.next();
//            if("1".equals(p.getAge())){
//                it.remove();
//            }
            System.out.println(it.next());
        }
    }
}
class Person implements Comparator<Person> {
    int age;
    String name;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Person(){

    }
    public Person(String name,int age){
        this.name=name;
        this.age=age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

    public int compare(Person o1, Person o2) {
        if(o1.age>o2.age){//年龄正序排列
            return 1;
        }else if(o1.age<o2.age)
        {
            return -1;
        }else{
            return 0;
        }
    }
//    public int compare(Person o1, Person o2) {//年龄倒置排序
//        if(o1.age<o2.age){//年龄正序排列
//            return 1;
//        }else if(o1.age>o2.age)
//        {
//            return -1;
//        }else{
//            return 0;
//        }
//    }
}